package com.jp.knowledge.my.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import com.jiayen.util.ToasUtil;
import com.jp.knowledge.R;
import com.jp.knowledge.comm.SlidingActivity;
import com.jp.knowledge.my.adapter.OptionSelectAdapter;
import com.jp.knowledge.my.base.RecycleVAdapter;
import com.jp.knowledge.my.business.OptionBusiness;
import com.jp.knowledge.my.business.Vector;
import com.jp.knowledge.my.model.OptionSelectModel;
import com.jp.knowledge.my.util.MyUtil;
import com.jp.knowledge.my.view.TradeCreateDialog;
import com.jp.knowledge.view.RecycleViewDiver;

import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import java.util.List;

/**
 * 公司规模/
 *
 * @author gzejia 978862664@qq.com
 */
public class OptionSelectActivity extends SlidingActivity {

    /**
     * 公司规模
     */
    public static final int OPTION_COMPANY_SIZE = 0;
    /**
     * 选择部门
     */
    public static final int OPTION_COMPANY_TRADE = 1;
    /**
     * 选择入学年份
     */
    public static final int OPTION_SCHOOL_START = 2;
    /**
     * 选择入班级
     */
    public static final int OPTION_FACULTY_CLASS = 3;

    private int OPTION_TYPE = OPTION_COMPANY_SIZE;

    @ViewInject(R.id.option_rv_select)
    private RecyclerView mOptionRv;

    @ViewInject(R.id.option_btn_create)
    private Button mCreateBtn;

    private OptionBusiness mBusiness = new OptionBusiness();

    @Override
    protected int getResourceId() {
        return R.layout.activity_option_select;
    }

    @Override
    protected void init() {
        initOthers();
        initTitle();
        initOptionRv(mBusiness.getOptions(OPTION_TYPE));
    }

    @Override
    protected void onPause() {
        super.onPause();
        MyUtil.closeKeyword(this);
    }

    @Event(value = {R.id.icon_left, R.id.option_btn_create}, type = View.OnClickListener.class)
    private void onClick(View view) {
        switch (view.getId()) {
            case R.id.icon_left:// 返回
                finish();
                break;
            case R.id.option_btn_create:// 创建组织
                showTradeCreate();
                break;
        }
    }

    private void initTitle() {
        topName.setText(OPTION_TYPE == OPTION_COMPANY_SIZE ?// 公司规模
                R.string.join_company_tv_size : OPTION_TYPE == OPTION_COMPANY_TRADE ?// 选择部门
                R.string.join_company_tv_class : OPTION_TYPE == OPTION_SCHOOL_START ?// 选择入学年份
                R.string.join_school_tv_start : OPTION_TYPE == OPTION_FACULTY_CLASS ?// 选择入班级
                R.string.join_school_tv_class : 0);
        topDesc.setVisibility(View.GONE);
        leftIcon.setImageResource(R.mipmap.arrow_white_left);
        rightIcon.setVisibility(View.GONE);
    }

    private void initOptionRv(final List<OptionSelectModel> models) {
        mOptionRv.setHasFixedSize(true);
        mOptionRv.setLayoutManager(new LinearLayoutManager(this));
        mOptionRv.addItemDecoration(new RecycleViewDiver(this, LinearLayout.HORIZONTAL));
        OptionSelectAdapter mSelectAdapter = new OptionSelectAdapter(this, models);
        mSelectAdapter.addItemClickListener(new RecycleVAdapter.OnItemClickListener() {
            @Override
            public void itemSelect(int position) {
                Intent intent = new Intent();
                intent.putExtra(Vector.RESULT_STRING + "optionContent",
                        models.get(position).mContent);
                setResult(RESULT_OK, intent);
                finish();
            }
        });

        mOptionRv.setAdapter(mSelectAdapter);
    }

    private void initOthers() {
        OPTION_TYPE = getIntent().getIntExtra(Vector.TRANSFER_INTEGER + "option_type",
                OPTION_COMPANY_SIZE);
        mCreateBtn.setVisibility(OPTION_TYPE == OPTION_COMPANY_TRADE // 公司部门
                || OPTION_TYPE == OPTION_FACULTY_CLASS // 院系班级
                ? View.VISIBLE : View.GONE);
    }

    private void showTradeCreate() {
        final TradeCreateDialog.Builder builder = new TradeCreateDialog.Builder(this);
        builder.setTitle(R.string.dialog_tv_hind);
        builder.addPositiveBtn(R.string.dialog_btn_cancel, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                MyUtil.closeKeyword(OptionSelectActivity.this);
                dialog.cancel();
            }
        });
        builder.addNegativeBtn(R.string.dialog_btn_sure, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (TextUtils.isEmpty(builder.getMessage())) {
                    ToasUtil.toast(OptionSelectActivity.this, "请确认输入内容");
                    return;
                }
                dialog.cancel();

                Intent intent = new Intent();
                intent.putExtra(Vector.RESULT_STRING + "optionContent", builder.getMessage());
                setResult(RESULT_OK, intent);
                finish();
            }
        });
        builder.create().show();
    }
}
